package com.daka.service.impl;

import com.daka.dao.GroupDao;
import com.daka.dao.VolunteerDao;
import com.daka.dao1.MemberOrgDao;
import com.daka.dao1.OrganizationDao;
import com.daka.entity.Group;
import com.daka.entity.MemberOrg1;
import com.daka.entity.Organization1;
import com.daka.entity.Volunteer;
import com.daka.service.GroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/11/22 10:55
 */

@Service("groupService")
public class GroupServiceImpl implements GroupService {

    private static final Logger logger = LoggerFactory.getLogger(GroupServiceImpl.class);

    @Resource
    private GroupDao groupDao;
    @Resource
    private VolunteerDao volunteerDao;
    @Resource
    private OrganizationDao organizationDao1;
    @Resource
    private MemberOrgDao memberOrgDao;


    public int deleteByPrimaryKey(Long id) {
        return groupDao.deleteByPrimaryKey(id);
    }

    public int insert(Group record) {
        return groupDao.insert(record);
    }

    public Group selectByPrimaryKey(Long id) {
        return groupDao.selectByPrimaryKey(id);
    }

    public Group selectByOldId(Long id) {
        return groupDao.selectByOldId(id);
    }

    public int updateByPrimaryKeySelective(Group record) {
        return groupDao.updateByPrimaryKeySelective(record);
    }

    public int groupOrg(Integer max, Integer min) {
        max = 11000;
        min = 0;
        Integer seed = 2000;

        int index = (max - min) / seed;
        int a = 0;
        for (int i = 1; i <= index + 1; i++) {

            int pMin = (seed * (i - 1)) + min;
            int pMax = (seed * (i - 1)) + seed + min - 1;
            logger.info("Volunteer 第{" + i + "}批，" + pMin + " 到" + pMax + "条");

            //查询
            List<Organization1> list = organizationDao1.selectList(pMax, pMin);
            logger.info("【查询条数】：{}", list.size());
            List<Group> list1 = new ArrayList<Group>();
            Group group;
            for (Organization1 org : list) {
                group = Group.build(org);

                if (org.getCreateuser() != null) {
                    Volunteer volunteer = volunteerDao.selectByOldId(org.getCreateuser());
                    if (volunteer != null) {
                        group.setVolunteerId(volunteer.getId());
                    }
                } else {//查询管理员
                    List<MemberOrg1> list2 = memberOrgDao.selectByOrg(org.getId());
                    if (list2.size() > 0) {
                        for (MemberOrg1 memberOrg1 : list2) {
                            if (memberOrg1.getAuthor() != null) {
                                if (memberOrg1.getAuthor().equalsIgnoreCase("11111")) {//创建者
                                    Volunteer volunteer1 = volunteerDao.selectByOldId(memberOrg1.getMember());
                                    if (volunteer1 != null) {
                                        group.setVolunteerId(volunteer1.getId());
                                    }
                                } else if (memberOrg1.getAuthor().contains("1")) {//管理员
                                    if (group.getVolunteerId() == null || group.getVolunteerId() == 0) {
                                        Volunteer volunteer1 = volunteerDao.selectByOldId(memberOrg1.getMember());
                                        if (volunteer1 != null) {
                                            group.setVolunteerId(volunteer1.getId());
                                        }
                                    }
                                }
                            }
                        }
                        if (group.getVolunteerId() == null || group.getVolunteerId() == 0) {
                            Volunteer volunteer1 = volunteerDao.selectByOldId(list2.get(0).getMember());
                            if (volunteer1 != null) {
                                group.setVolunteerId(volunteer1.getId());
                            }
                        }
                    }
                }
                list1.add(group);
            }
            //插入
            if (list1.size() > 0) {
                a = groupDao.insertList(list1);
            }
            logger.info("【插入条数】：{}", a);
        }
        return a;
    }

    public void test() {
        logger.info("");
    }

}





